package com.demo.webapp.ui.menu.taglib.el;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.Authentication;

import com.demo.webapp.ui.access.PermissionsAdapter;
import com.demo.webapp.ui.menu.taglib.el.MenuDisplayerTag;
import com.demo.webapp.ui.utils.ExpressionEvaluator;

import javax.servlet.jsp.JspException;

/**
 * Menu的Jsp标签,允许使用EL表达式语言
 * 标签使用：
 * <pre>
 *  &lt;menu:menuDisplayer name="ListMenu"&gt;
 *     &lt;menu:menu name="MyMenu"/&gt;
 *  &lt;/menu:menuDisplayer&gt;
 * </pre>
 * 
 * @author ryuu.kk
 * @version 1.16 @at 2012/02/20 
 */
public class MenuDisplayerTag extends com.demo.webapp.ui.menu.taglib.MenuDisplayerTag {
    //~ Static fields/initializers =============================================

    /**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = -5364216398273906003L;
	/** 日志 **/
	private static Log log = LogFactory.getLog(MenuDisplayerTag.class);

	public int doStartTag() throws JspException {
    	//EL
    	evaluateExpressions();
    	//使用超类方法
        return super.doStartTag();    }

    protected PermissionsAdapter<Authentication> getPermissionsAdapter()
            throws JspException {
        PermissionsAdapter<Authentication> adapter = null;

        if (permissions != null) {
            // If set to "rolesAdapter", then create automatically
            if (permissions.equalsIgnoreCase(ROLES_ADAPTER)) {
                //adapter = new RolesPermissionsAdapter((HttpServletRequest) pageContext.getRequest());
            } else {
                //adapter = (PermissionsAdapter) pageContext.findAttribute(permissions);

                if (adapter == null) {
                    throw new JspException(messages.getString("permissions.not.found"));
                }
            }
        }

        return adapter;
    }

    public int doEndTag() throws JspException {
        menuDisplayer.end(pageContext);
        pageContext.removeAttribute(DISPLAYER_KEY);
        pageContext.removeAttribute(PRIVATE_REPOSITORY);
        return (EVAL_PAGE);
    }

    private void evaluateExpressions() throws JspException {
        ExpressionEvaluator eval = new ExpressionEvaluator(this, pageContext);
        
        if (log.isDebugEnabled()) {
        	log.debug(String.format("look at the key [name:%s, config:%s, localeKey:%s, bundleKey:%s, permissions:%s, repository:%s]", 
        			name, config, localeKey, bundleKey, permissions, repository));
        }
        
        if (name != null) {
            super.setName(eval.evalString("name", name));
        }

        if (config != null) {
            super.setConfig(eval.evalString("config", config));
        }

        if (localeKey != null) {
            super.setLocale(eval.evalString("localeKey", localeKey));
        }

        if (bundleKey != null) {
            super.setBundle(eval.evalString("bundleKey", bundleKey));
        }
        
        if (permissions != null) {
            super.setPermissions(eval.evalString("permissions", permissions));
        }

        if (repository != null) {
            super.setRepository(eval.evalString("repository", repository));
        }
//        if (log.isDebugEnabled()) {
//        	log.debug(String.format("look at the value [name:%s, config:%s, localeKey:%s, bundleKey:%s, permissions:%s, repository:%s]", 
//        			name, config, localeKey, bundleKey, permissions, repository));
//        }
    }
}
